package com.example.hms.service.servicelmpl;

import com.example.hms.dao.CustomerDao;
import com.example.hms.entity.Customer;
import com.example.hms.entity.SystemLog;
import com.example.hms.service.CustomerService;
import com.example.hms.service.SystemLogService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class CustomerServiceImpl implements CustomerService {
    @Resource
    CustomerDao customerDao;
    @Resource
    SystemLogService systemLogService;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Result selectAllCustomer(String customerName,String phoneNumber,String identificationCard) {
        List<Customer> customer = customerDao.selectAllCustomer(customerName, phoneNumber, identificationCard);
        return ResultSubmit.success(customer);
    }

    @Override
    public Result insert(Customer customer, HttpServletRequest request) {
        customer.setDataUpdateTime(format.format(new Date()));
        customer.setCustomerId(Math.abs((int)System.currentTimeMillis()));
        customer.setDeleteIdentity("未删除");
        int insert = customerDao.insert(customer);
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("customer");
        systemLog.setUseTableDataIdAfter(String.valueOf(
                customerDao.selectAllCustomer(customer.getCustomerName(), customer.getPhoneNumber(),customer.getIdentificationCard()).
                        get(0).getCustomerTableId()));
        systemLog.setUseType("顾客新增");
        systemLogService.insert(systemLog);
        if (insert!=0){
            return ResultSubmit.success("添加成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("添加失败");
        }
    }

    @Override
    public Result update(Customer customer, HttpServletRequest request) {
        int update1 = customerDao.update(customer);
        customer.setDataUpdateTime(format.format(new Date()));
        customer.setCustomerId(Math.abs((int)System.currentTimeMillis()));
        customer.setDeleteIdentity("未删除");
        int insert1 = customerDao.insert(customer);
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("customer");
        systemLog.setUseTableDataIdBefore(String.valueOf(
                customerDao.selectAllCustomer(customer.getCustomerName(), customer.getPhoneNumber(),customer.getIdentificationCard()).
                        get(0).getCustomerTableId()));
        systemLog.setUseTableDataIdAfter(String.valueOf(
                customerDao.selectAllCustomer(customer.getCustomerName(), customer.getPhoneNumber(),customer.getIdentificationCard()).
                        get(0).getCustomerTableId()));
        systemLog.setUseType("顾客修改");
        systemLogService.insert(systemLog);
        if (update1==1 || insert1==1){
            return ResultSubmit.success("修改成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("修改失败");
        }
    }
    @Override
    public Result delete(Customer customer, HttpServletRequest request) {
        Integer tableId =customerDao.selectAllCustomer(customer.getCustomerName(), customer.getPhoneNumber(),customer.getIdentificationCard()).
                get(0).getCustomerTableId();
        int delete1 = customerDao.update(customer);

        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("customer");
        systemLog.setUseTableDataIdBefore(String.valueOf(customer.getCustomerTableId()));
        systemLog.setUseType("顾客删除");
        systemLogService.insert(systemLog);

        if (delete1!=0){
            return ResultSubmit.success("执行成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("执行失败");
        }
    }
}
